  var tt_days, dmin, dmax;
jQuery(function($) {
  //( Points Remaining / ( points per hour * (( percentual /100)+1)))*3600000
  
  // Science
  var r = $('#awplus_content table:eq(0) tr td:eq(4)').text().match(/([-+0-9]+)/g);

  if (r.length > 1) {
    var science_per = parseInt(r[0]) * ((parseInt(r[1])/100)+1);
    $('table:eq(2) tr td:eq(4)').append(' &nbsp;= '+Math.floor(science_per));
  } else {
    var science_per = parseInt(r[0]);
  }
  
  // Culture
  var x = $('#awplus_content table:eq(0) tr:eq(8) td:eq(0)').text().match(/([-+0-9]+)/g);
  
  if (x.length > 1) {
    var culture_per = parseInt(x[0]) * ((parseInt(x[1])/100)+1);
    $('table:eq(2) tr:eq(8) td:eq(0)').append(' &nbsp;= '+Math.floor(culture_per));
  } else {
    var culture_per = parseInt(x[0]);
  }

  var rmn, rph, xo;
  xo = [];
  $('tr[bgcolor="#206060"] td:eq(3), tr:eq(9) td:eq(3)', 'table:eq(2)').each(function(i) {
    rmn = parseFloat($('input', this).size() ? $('input', this).val() : $(this).text());

    rph = i == 0 ? science_per : culture_per;

    var d = new Date();
    d.setTime(d.getTime() + ((rmn/rph) * 3600000));
    xo.push(d);
    delete d;
  });
  
  var html = '<div class="ui-helper-clearfix" style="margin-top: 10px">'
    + '<div class="ui-widget ui-widget-content ui-corner-all ui-dialog" style="float: left; width: 592px;">'
      + '<div class="ui-widget-header ui-corner-all ui-dialog-titlebar ui-helper-clearfix">'
        + '<span class="ui-dialog-title">Next level at:</span>'
        + '<span class="ui-icon ui-icon-calendar" style="float: right;"></span>'
      + '</div>'
      + '<div class="ui-dialog-content ui-widget-content">'
        + '<p><strong>Science:</strong> '+ xo[0].toString() + '</p>'
        + '<p><strong>Culture:</strong> '+ xo[1].toString() + '</p>'
      + '</div>'
    + '</div>'
    + '</div>';

  $('#awplus_content table:eq(0)').after(html);
  
  
  // Changing running science.
  
  $('#awplus_content table:first tr:gt(1):lt(6)').each(function() {
    if ($(this).attr('bgcolor') == '#206060') {
      $(this).prepend('<td> <div class="ui-icon ui-icon-check"></div> </td>');
      return;
    }
    
    $(this).prepend('<td><div class="ui-state-default ui-corner-all awplus_science_act"><div class="ui-icon ui-icon-play"></div></div></td>');
  });
  
  // Fixing table
  $('#awplus_content table:first tr:first td:first').attr('width', 30);
  $('#awplus_content table:first tr:eq(1) td:first').attr('colspan', 2);
  $('#awplus_content table:first tr:eq(8) td:first').attr('colspan', 6);
  $('#awplus_content table:first tr:eq(9) td:first').attr('colspan', 2);
  
  // Actions
  var ap_sciences = ['f_bio', 'f_eco', 'f_energy', 'f_math', 'f_physics', 'f_social'];
  $('td:first', '#awplus_content table:first tr:gt(1):lt(6)').each(function(i) {
    $('div.awplus_science_act', this)
      .hover(
        function() { $(this).addClass('ui-state-hover').css('cursor', 'pointer'); },
        function() { $(this).removeClass('ui-state-hover'); }
      )
      .click(function () {
        $(this).addClass('ui-state-active');
        awPlus.change_science(ap_sciences[i]);
      });
  });

});
